<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>promise的使用 | 地球反三体组织</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="/icons/favicon.ico">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
    <meta name="description" content="">
    
    <link rel="preload" href="/assets/css/0.styles.95f09eca.css" as="style"><link rel="preload" href="/assets/js/app.892efb48.js" as="script"><link rel="preload" href="/assets/js/3.3689c535.js" as="script"><link rel="preload" href="/assets/js/1.76946483.js" as="script"><link rel="preload" href="/assets/js/30.cdb0bfe0.js" as="script"><link rel="prefetch" href="/assets/js/10.994dfc46.js"><link rel="prefetch" href="/assets/js/11.5243d02b.js"><link rel="prefetch" href="/assets/js/12.bbc947db.js"><link rel="prefetch" href="/assets/js/13.3c555110.js"><link rel="prefetch" href="/assets/js/14.791ae1f2.js"><link rel="prefetch" href="/assets/js/15.41a71b1f.js"><link rel="prefetch" href="/assets/js/16.ea9f6e56.js"><link rel="prefetch" href="/assets/js/17.8d9b3f45.js"><link rel="prefetch" href="/assets/js/18.990bf501.js"><link rel="prefetch" href="/assets/js/19.37c1c21a.js"><link rel="prefetch" href="/assets/js/20.5aae01e6.js"><link rel="prefetch" href="/assets/js/21.41e758f1.js"><link rel="prefetch" href="/assets/js/22.afc2de9d.js"><link rel="prefetch" href="/assets/js/23.7a76c57a.js"><link rel="prefetch" href="/assets/js/24.ee89a782.js"><link rel="prefetch" href="/assets/js/25.aae5f43e.js"><link rel="prefetch" href="/assets/js/26.eec74c57.js"><link rel="prefetch" href="/assets/js/27.cdae5890.js"><link rel="prefetch" href="/assets/js/28.85c693b7.js"><link rel="prefetch" href="/assets/js/29.d40f3724.js"><link rel="prefetch" href="/assets/js/31.f83a512c.js"><link rel="prefetch" href="/assets/js/32.87779683.js"><link rel="prefetch" href="/assets/js/33.e9f606e7.js"><link rel="prefetch" href="/assets/js/34.6b5688ff.js"><link rel="prefetch" href="/assets/js/35.a0ba89c1.js"><link rel="prefetch" href="/assets/js/36.18a00568.js"><link rel="prefetch" href="/assets/js/37.fd83be59.js"><link rel="prefetch" href="/assets/js/38.cf0ec149.js"><link rel="prefetch" href="/assets/js/39.3a424296.js"><link rel="prefetch" href="/assets/js/4.4ef4e9bb.js"><link rel="prefetch" href="/assets/js/40.3ac4f22a.js"><link rel="prefetch" href="/assets/js/41.f4119124.js"><link rel="prefetch" href="/assets/js/42.926201e5.js"><link rel="prefetch" href="/assets/js/43.b8af425d.js"><link rel="prefetch" href="/assets/js/44.b5af9721.js"><link rel="prefetch" href="/assets/js/45.2cf05349.js"><link rel="prefetch" href="/assets/js/46.6ef53d5a.js"><link rel="prefetch" href="/assets/js/47.0c0d1d6f.js"><link rel="prefetch" href="/assets/js/48.afcf25ca.js"><link rel="prefetch" href="/assets/js/49.56f7da91.js"><link rel="prefetch" href="/assets/js/5.152764ca.js"><link rel="prefetch" href="/assets/js/50.6dbb0658.js"><link rel="prefetch" href="/assets/js/6.8ab23831.js"><link rel="prefetch" href="/assets/js/7.b1119581.js"><link rel="prefetch" href="/assets/js/8.ebb978a3.js"><link rel="prefetch" href="/assets/js/9.a72af8f5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.95f09eca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-73b6f1b4><div data-v-73b6f1b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-73b6f1b4 data-v-73b6f1b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-58d5ab76 data-v-73b6f1b4 data-v-73b6f1b4><h3 class="title" style="display:none;" data-v-58d5ab76 data-v-58d5ab76>地球反三体组织</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><input type="password" value="" data-v-58d5ab76> <span data-v-58d5ab76>Konck! Knock!</span> <button data-v-58d5ab76>OK</button></label> <div class="footer" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><span data-v-58d5ab76><i class="iconfont reco-theme" data-v-58d5ab76></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-58d5ab76>vuePress-theme-reco</a></span> <span data-v-58d5ab76><i class="iconfont reco-copyright" data-v-58d5ab76></i> <a data-v-58d5ab76><span data-v-58d5ab76>kuangw</span>
            
          <!---->
          2020
        </a></span></div></div> <div class="hide" data-v-73b6f1b4><header class="navbar" data-v-73b6f1b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <div class="pt-bgBar"></div> <a href="/" class="home-link router-link-active"><img src="/images/logo.png" alt="地球反三体组织" class="logo"> <span class="site-name">地球反三体组织</span></a> <div class="links"><div><div class="my-color isDrak"><div class="div1"><img src="http://photo.tuituisoft.com/picgo/20201202095920.png" alt></div> <div class="div2"><img src="http://photo.tuituisoft.com/picgo/20201202095945.png" alt></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont iconfont iconhome"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/book/" class="nav-link"><i class="iconfont undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/categories/code/" class="nav-link"><i class="iconfont undefined"></i>
  code
</a></li><li class="dropdown-item"><!----> <a href="/categories/tool/" class="nav-link"><i class="iconfont undefined"></i>
  tool
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont undefined"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tool/vuepress/" class="nav-link"><i class="iconfont undefined"></i>
  vuepress(博客框架)
</a></li><li class="dropdown-item"><!----> <a href="/tool/picgo.html" class="nav-link"><i class="iconfont undefined"></i>
  PicGo(图床工具)
</a></li><li class="dropdown-item"><h4>vscode</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/tool/vscode/plug-recommend.html" class="nav-link"><i class="iconfont undefined"></i>
  vscode插件推荐
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/diy-code.html" class="nav-link"><i class="iconfont undefined"></i>
  如何自定义vscode的代码块
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/code-block.html" class="nav-link"><i class="iconfont undefined"></i>
  常用代码块
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div> <a href="https://gitee.com/kuangweiwudi/my-node" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask" data-v-73b6f1b4></div> <aside class="sidebar" data-v-73b6f1b4><div class="personal-info-wrapper" data-v-3eba7259 data-v-73b6f1b4><img src="http://image.woai996.com/picgo/20201127101131.png" alt="author-avatar" class="personal-img" data-v-3eba7259> <h3 class="name" data-v-3eba7259>
    kuangw
  </h3> <div class="num" data-v-3eba7259><div data-v-3eba7259><h3 data-v-3eba7259>29</h3> <h6 data-v-3eba7259>Article</h6></div> <div data-v-3eba7259><h3 data-v-3eba7259>22</h3> <h6 data-v-3eba7259>Tag</h6></div></div> <hr data-v-3eba7259></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont iconfont iconhome"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/book/" class="nav-link"><i class="iconfont undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/categories/code/" class="nav-link"><i class="iconfont undefined"></i>
  code
</a></li><li class="dropdown-item"><!----> <a href="/categories/tool/" class="nav-link"><i class="iconfont undefined"></i>
  tool
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont undefined"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tool/vuepress/" class="nav-link"><i class="iconfont undefined"></i>
  vuepress(博客框架)
</a></li><li class="dropdown-item"><!----> <a href="/tool/picgo.html" class="nav-link"><i class="iconfont undefined"></i>
  PicGo(图床工具)
</a></li><li class="dropdown-item"><h4>vscode</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/tool/vscode/plug-recommend.html" class="nav-link"><i class="iconfont undefined"></i>
  vscode插件推荐
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/diy-code.html" class="nav-link"><i class="iconfont undefined"></i>
  如何自定义vscode的代码块
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/code-block.html" class="nav-link"><i class="iconfont undefined"></i>
  常用代码块
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div> <a href="https://gitee.com/kuangweiwudi/my-node" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-58d5ab76 data-v-73b6f1b4><h3 class="title" style="display:none;" data-v-58d5ab76 data-v-58d5ab76>promise的使用</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><input type="password" value="" data-v-58d5ab76> <span data-v-58d5ab76>Konck! Knock!</span> <button data-v-58d5ab76>OK</button></label> <div class="footer" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><span data-v-58d5ab76><i class="iconfont reco-theme" data-v-58d5ab76></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-58d5ab76>vuePress-theme-reco</a></span> <span data-v-58d5ab76><i class="iconfont reco-copyright" data-v-58d5ab76></i> <a data-v-58d5ab76><span data-v-58d5ab76>kuangw</span>
            
          <!---->
          2020
        </a></span></div></div> <div data-v-73b6f1b4><main class="page"><div class="page-title" style="display:none;position:relative;"><div class="page-bg-img"><img src="https://w.wallhaven.cc/full/g7/wallhaven-g7zl1e.jpg"></div> <h1>promise的使用</h1> <hr> <div data-v-523cfaee><i class="iconfont reco-account" data-v-523cfaee><span data-v-523cfaee>kuangw</span></i> <i class="iconfont reco-date" data-v-523cfaee><span data-v-523cfaee>2020-12-02</span></i> <i class="iconfont reco-eye" data-v-523cfaee><span id="/js/catelog-node/promise-use.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-523cfaee><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#d6dce5;"></a></span></i> <i class="iconfont reco-tag tags" data-v-523cfaee><span class="tag-item" data-v-523cfaee>js</span><span class="tag-item" data-v-523cfaee>前端</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="为什么要使用-promise"><a href="#为什么要使用-promise" class="header-anchor">#</a> 为什么要使用 promise？</h2> <ol><li>promise 专门为了处理<strong>回调函数</strong>而生，除了回调函数，也可以写出漂亮而优雅的<strong>链式编程</strong></li> <li>避免陷入<strong>回调地狱</strong></li> <li>使代码变得更加的<strong>优雅</strong></li> <li>代码的可维护性变高</li> <li>promise是现代js最值得学习的技术之一</li></ol> <h2 id="对比-promise-函数和传统函数"><a href="#对比-promise-函数和传统函数" class="header-anchor">#</a> 对比 Promise 函数和传统函数</h2> <details class="custom-block details"><summary>展开查看</summary> <ul><li>传统函数</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">fn</span><span class="token punctuation">(</span>prams<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// code</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>promise 函数</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">fn</span><span class="token punctuation">(</span>prams<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// code</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><blockquote><p>是不是感觉没有什么区别？这是因为上面定义的都是是极简的函数，如果嵌套再多几层，你就会大呼<code>promise真香了</code>,下面我们再做对比</p></blockquote> <ul><li>传统函数</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> <span class="token function-variable function">fn1</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">prams<span class="token punctuation">,</span> callback</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;函数1执行完毕&quot;</span><span class="token punctuation">,</span> prams<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn2</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">prams<span class="token punctuation">,</span> callback</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;函数2执行完毕&quot;</span><span class="token punctuation">,</span> prams<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn3</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">prams<span class="token punctuation">,</span> callback</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;函数3执行完毕&quot;</span><span class="token punctuation">,</span> prams<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">// 上面我们使用定时器来模拟几个异步函数</span>
<span class="token function">fn1</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数1的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">fn2</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数2的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">fn3</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数3的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">// 可以看到我们已经陷入的嵌套的地狱中了，这里这是模拟，如果代码再复杂一点,维护就会变得痛苦不堪</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>Promise 写法</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> <span class="token function-variable function">fn1</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;函数1执行完毕&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn2</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;函数2执行完毕&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn3</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;函数3执行完毕&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 定义三个Promise函数，具体如何定义在下面会再介绍，这里我们只需要对比两种写法的直观性</span>

<span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数1的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">fn2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数2的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">fn3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数3的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></details> <h2 id="定义-promise-函数"><a href="#定义-promise-函数" class="header-anchor">#</a> 定义 promise 函数</h2> <div class="custom-block tip"><p>定义一个 promise 函数一点也不复杂 ，我们只需在传统函数中 <code>returen</code> 一个 promise 实例
<br></p></div> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//  Promise实例的 resolve, reject 参数，分别代表着成功和失败的回调函数</span>
<span class="token keyword">let</span> <span class="token function-variable function">promiseFun</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 这里还是使用定时器来模拟异步</span>
    <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">let</span> randomNum <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">// 为了模拟场景不确定性，我们使用一个随机数只要随机数&gt;0.5 触发 成功的函数，否则失败</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>randomNum <span class="token operator">&gt;</span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">resolve</span><span class="token punctuation">(</span>randomNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token function">reject</span><span class="token punctuation">(</span>randomNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 定义好了之后进行调用</span>
<span class="token comment">// .then 捕获成功的函数</span>
<span class="token comment">// .catch 捕获失败的函数</span>
<span class="token function">promiseFun</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;成功了&quot;</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;失败了&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="promise-all-的使用方法"><a href="#promise-all-的使用方法" class="header-anchor">#</a> Promise.all 的使用方法</h2> <div class="custom-block tip"><p>promise.all 专门用于同时处理多个异步操做 , 参数是一个数组,数组里可以传入若干个 promise 函数</p></div> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// promiseFun1(),promiseFun2() 和上面的写法是一样，使用定时器+随机数的组合</span>
Promise<span class="token punctuation">.</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token function">promiseFun1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">promiseFun2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;成功了&quot;</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;失败了&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 注意 ：</span>
<span class="token comment">// 只有当数组里面所有的promise调用了resolve的函数</span>
<span class="token comment">// then()函数才会开始调用 res参数是一个数组,放着所有函数的回调值</span>

<span class="token comment">// 但只要其中一个调用了reject函数</span>
<span class="token comment">// catch就会开始发挥作用 err是调用reject那个Promise函数的回调值</span>
</code></pre></div><h2 id="async-和-await"><a href="#async-和-await" class="header-anchor">#</a> async 和 await</h2> <div class="custom-block tip"><p><code>aysnc\await</code>是 es7 更新中 , js 对于 Promise 函数的一种语法糖
<br>
可以说是 js 解决目前解决回调函数的<strong>终极答案</strong> <br>
有了它,我们甚至不用理会 Promise.all()</p></div> <h3 id="async-和-promise-函数的使用对比"><a href="#async-和-promise-函数的使用对比" class="header-anchor">#</a> async 和 Promise 函数的使用对比</h3> <blockquote><p>例子中使用的函数还是上面我们定义的形式,我们这里暂时不考虑<code>.catch</code>的情况</p></blockquote> <ul><li>promise 的普通使用形式</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数1的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">fn2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数2的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">fn3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是函数3的回调函数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 这还不算优雅</span>
</code></pre></div><ul><li>async 的使用</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> res1 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> res2 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fn2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> res3 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fn3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 这才是终极优雅</span>
</code></pre></div><h3 id="aysnc-await-的使用"><a href="#aysnc-await-的使用" class="header-anchor">#</a> aysnc\await 的使用</h3> <blockquote><p>它们的使用甚至不需要进行详细的解释,让我们来看上面的代码</p></blockquote> <div class="custom-block warning"><p class="custom-block-title">使用它们的要点</p> <ol><li><code>async</code> 和 <code>await</code> 必定是成双出现的 , await 必须要在函数中并且是 async 的函数中使用,否则就会报错</li> <li><code>await</code>后面跟着的必定是 promise 函数, js 解析的时候遇到<code>await</code> 就会等函数的<code>reslove</code>调用才会往下继续执行</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> res <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 在fn1的resolve没调用时,下面的代码不会执行</span>
  <span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// res === res</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>要捕捉<code>reject</code>需要使用 <code>try...catch</code></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">try</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> res1 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> res2 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fn2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> res3 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fn3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 如果以上有某个函数调用了reject 这里就会触发</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="4"><li><code>async</code>函数本质其实也是一个 Promise 的实例对象,所以你要获得这种函数的返回值,也要使用<code>.then</code>的方法</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">fn1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><img src="http://photo.tuituisoft.com/picgo/20201202171445.png" alt="alt"> <br></p> <ol start="5"><li>所以 在async函数中 <code>return</code> 和 <code>throw</code> 对应着Promise的<code>resolve</code>和<code>reject</code></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">fun1</span><span class="token punctuation">(</span><span class="token parameter">falg</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>falg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token string">&quot;aaa&quot;</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token string">&quot;bbb&quot;</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">fun1</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="6"><li>但似乎它并没有处理异步的能力</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">fun1</span><span class="token punctuation">(</span><span class="token parameter">falg</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>falg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token keyword">return</span> <span class="token string">&quot;aaa&quot;</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
				<span class="token keyword">throw</span> <span class="token string">&quot;bbb&quot;</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>

	<span class="token comment">// 我们不能这样 因为这是在另一个函数内了 </span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>


</code></pre></div></div> <h2 id="最后"><a href="#最后" class="header-anchor">#</a> 最后</h2> <p>如果要使用好promise，还是需要不断的实践</p></div> <footer class="page-edit" style="display:none;"><div class="edit-link"><a href="https://gitee.com/kuangweiwudi/my-node/edit/master/js/catelog-node/promise-use.md" target="_blank" rel="noopener noreferrer">提出错误！</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <!----> <!----> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-3fb51744><li class="level-2" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#为什么要使用-promise" class="sidebar-link" data-v-3fb51744>为什么要使用 promise？</a></li><li class="level-2" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#对比-promise-函数和传统函数" class="sidebar-link" data-v-3fb51744>对比 Promise 函数和传统函数</a></li><li class="level-2" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#定义-promise-函数" class="sidebar-link" data-v-3fb51744>定义 promise 函数</a></li><li class="level-2" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#promise-all-的使用方法" class="sidebar-link" data-v-3fb51744>Promise.all 的使用方法</a></li><li class="level-2" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#async-和-await" class="sidebar-link" data-v-3fb51744>async 和 await</a></li><li class="level-3" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#async-和-promise-函数的使用对比" class="sidebar-link" data-v-3fb51744>async 和 Promise 函数的使用对比</a></li><li class="level-3" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#aysnc-await-的使用" class="sidebar-link" data-v-3fb51744>aysnc\await 的使用</a></li><li class="level-2" data-v-3fb51744><a href="/js/catelog-node/promise-use.html#最后" class="sidebar-link" data-v-3fb51744>最后</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.892efb48.js" defer></script><script src="/assets/js/3.3689c535.js" defer></script><script src="/assets/js/1.76946483.js" defer></script><script src="/assets/js/30.cdb0bfe0.js" defer></script>
  </body>
</html>
